
/*头文件引用*/
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/pid.h>
#include <linux/wait.h>
#include <linux/list.h>
#include <linux/kthread.h>
#include <linux/delay.h>
MODULE_LICENSE("GPL");
/*全局变量定义*/
static wait_queue_head_t head;  //等待队列头元素
struct task_struct * old_thread; //保存进程描述符信息

int my_function(void * argc)
{
	printk("in the kernel thread function! \n");
	printk("the current pid is:%d\n", current->pid);    //显示当前进程的PID值
// 显示init进程的状态
	printk("the state of the init function is :%ld\n", old_thread->state);
	__wake_up(&head, TASK_ALL,0, NULL);                   //调用函数唤醒等待队列中的进程
//显示函数__wake_up( )调用之后的init进程的状态
	printk("the state of the init function after __wake_up is :%ld\n", old_thread->state);
	printk("out the kernel thread function\n");
	return 0;
}


static int __init __wake_up_init(void)
{
	char namefrm[]="__wake_up.c";                 //线程的输出类型名，在此程序中无影响
	long time_out;
	struct task_struct * result;                   //进程信息结构体变量
	wait_queue_t data;                              //等待队列元素
	printk("into __wake_up_init.\n");
	result=kthread_create_on_node(my_function, NULL, -1, namefrm); //创建新进程
	printk("the pid of the new thread is:%d\n", result->pid); //显示新线程的PID值
	printk("the current pid is:%d\n", current->pid);           //显示当前进程的PID值
	init_waitqueue_head(&head);                     //初始化等待队列头元素
	init_waitqueue_entry(&data, current);           //用当前进程初始化等待队列中的一个元素
	add_wait_queue(&head, &data);                   //将等待队列元素加入等待队列中
	old_thread=current;                             //记录当前进程的信息
	wake_up_process(result);                        //唤醒新创建的线程
	time_out=schedule_timeout_uninterruptible(1000*10);     //让当前进程进入睡眠状态
	printk("the schedule timeout is:%ld\n", time_out);       //显示函数返回结果
	printk("out __wake_up_init.\n");
	return 0;
}

static void __exit __wake_up_exit(void)
{
printk("Goodbye __wake_up\n");
}



module_init(__wake_up_init);
module_exit(__wake_up_exit);


